package com.xiaomengkuan.plugin.service.impl;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.xiaomengkuan.common.exception.CasualException;
import com.xiaomengkuan.plugin.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-01-13 10:12 下午, File Encoding: utf8 --
 */
@Service
public class SmsServiceImpl implements SmsService {
    @Value("${qcloud.cos.secretId}")
    private String secretId;
    @Value("${qcloud.cos.secretKey}")
    private String secretKey;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public String sendMessage(String phone) {
        try {
            String s = redisTemplate.opsForValue().get(phone);
            if (StringUtils.isEmpty(s)) {
                String code = generateCode();
                Credential cred = new Credential(secretId, secretKey);
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.setConnTimeout(60);
                ClientProfile clientProfile = new ClientProfile();
                clientProfile.setSignMethod("HmacSHA256");
                clientProfile.setHttpProfile(httpProfile);
                SmsClient client = new SmsClient(cred, "", clientProfile);
                SendSmsRequest req = new SendSmsRequest();
                String appid = "1400487124";
                req.setSmsSdkAppid(appid);
                String sign = "xiaomengkuan";
                req.setSign(sign);
                String templateID = "875437";
                req.setTemplateID(templateID);
                String[] phoneNumbers = {"+86" + phone};
                req.setPhoneNumberSet(phoneNumbers);
                String[] templateParams = {code};
                req.setTemplateParamSet(templateParams);
                SendSmsResponse res = client.SendSms(req);
                System.out.println(SendSmsResponse.toJsonString(res));
                System.out.println(res.getRequestId());
                redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
                return code;
            }else {
                throw new CasualException(5000, "已经发送过验证码了");
            }
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return null;
        }
    }

    private String generateCode() {
        Random rand = new Random();
        String sources = "0123456789";
        StringBuilder code = new StringBuilder();
        for (int j = 0; j < 6; j++) {
            code.append(sources.charAt(rand.nextInt(9)));
        }
        return code.toString();
    }
}
